home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / PRODUCTREGISTRY.PY < prev    next >
Encoding:
Python Source  |  2000-01-10  |  9.7 KB  |  252 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64.  
  65. # Product registry and new product factory model.  There will be a new
  66. # mechanism for defining actions for meta types.  If an action is of
  67. # the form:
  68. #
  69. #  manage_addProduct-name-factoryid
  70. #
  71. # Then the machinery that invokes an add-product form
  72. # will return:
  73. # ....what?
  74.  
  75. from OFS.Folder import Folder
  76.  
  77. class ProductRegistryMixin:
  78.     # This class implements a protocol for registering products that
  79.     # are defined through the web.
  80.  
  81.     # This class is a mix-in class for the top-level application object.
  82.     
  83.     def _manage_remove_product_meta_type(self, product,
  84.                                          id=None, meta_type=None):
  85.         r=[]
  86.         pid=product.id
  87.         for mt in self._getProductRegistryMetaTypes():
  88.             if mt.has_key('product'):
  89.                 if mt['product']==pid and (
  90.                     meta_type is None or meta_type==mt['name']):
  91.                     continue
  92.                 elif meta_type==mt['name']: continue
  93.                 r.append(mt)
  94.             
  95.         self._setProductRegistryMetaTypes(tuple(r))
  96.  
  97.     def _constructor_prefix_string(self, pid):
  98.         return 'manage_addProduct/%s/' % pid
  99.         
  100.     def _manage_add_product_meta_type(self, product, id, meta_type,
  101.                                       permission=''):
  102.         pid=product.id
  103.  
  104.         meta_types=self._getProductRegistryMetaTypes()
  105.  
  106.         for mt in meta_types:
  107.             if mt['name']==meta_type:
  108.                 if not mt.has_key('product'): mt['product']=pid
  109.                 if mt['product'] != pid:
  110.                     raise 'Type Exists', (
  111.                         'The type <em>%s</em> is already defined.' % meta_type)
  112.                 mt['action']='%s%s' % (
  113.                     self._constructor_prefix_string(pid), id)
  114.                 if permission: mt['permission']=permission
  115.                 return
  116.  
  117.         mt={
  118.             'name': meta_type,
  119.             'action': ('%s%s' % (
  120.                 self._constructor_prefix_string(pid), id)),
  121.             'product': pid
  122.             }
  123.         if permission: mt['permission']=permission
  124.         
  125.         self._setProductRegistryMetaTypes(meta_types+(mt,))
  126.     
  127.     def _manage_remove_product_permission(self, product, permission=None):
  128.         r=[]
  129.         r2=[]
  130.         pid=product.id
  131.         for d in self._getProductRegistryData('permissions'):
  132.             if d.has_key('product'):
  133.                 if d['product']==pid and (
  134.                     permission is None or permission==d['name']):
  135.                     continue
  136.                 elif permission==d['name']: continue
  137.                 r.append(d)
  138.                 r2.append((d['name'], d['methods'], d['default']))
  139.             
  140.         self._setProductRegistryData('permissions', tuple(r))
  141.         self._setProductRegistryData('ac_permissions', tuple(r2))
  142.  
  143.     def _manage_add_product_permission(
  144.         self, product, permission, methods=(), default=('Manager',)
  145.         ):
  146.  
  147.         permissions=self._getProductRegistryData('permissions')
  148.  
  149.         for d in permissions:
  150.             if d['name']==permission:
  151.                 raise 'Type Exists', (
  152.                     'The permission <em>%s</em> is already defined.'
  153.                     % permission)
  154.         
  155.         d={'name': permission, 'methods': methods, 'permission': permission,
  156.                 'default': default, 'product': product.id}
  157.         
  158.         self._setProductRegistryData('permissions', permissions + (d,))
  159.         self._setProductRegistryData(
  160.             'ac_permissions',
  161.             self._getProductRegistryData('ac_permissions')
  162.             +((d['name'], d['methods'], d['default']),)
  163.             )
  164.  
  165.     # HACK - sometimes an unwrapped App object seems to be passed as
  166.     # self to these methods, which means that they dont have an aq_aquire
  167.     # method. Until Jim has time to look into this, this aq_maybe method
  168.     # appears to be an effective work-around...
  169.     def aq_maybe(self, name):
  170.         if hasattr(self, name):
  171.             return getattr(self, name)
  172.         return self.aq_acquire(name)
  173.  
  174.     def _manage_add_product_data(self, type, product, id, **data):
  175.         values=filter(
  176.             lambda d, product=product, id=id:
  177.             not (d['product']==product and d['id']==id),
  178.             list(self.aq_maybe('_getProductRegistryData')(type))
  179.             )
  180.  
  181.         data['product']=product
  182.         data['id']=id
  183.         values.append(data)
  184.  
  185.         self.aq_maybe('_setProductRegistryData')(type, tuple(values))
  186.  
  187.     def _manage_remove_product_data(self, type, product, id):
  188.         values=filter(
  189.             lambda d, product=product, id=id:
  190.             not (d['product'] in
  191.                  (product,
  192.                   'methods' # hack to get around inner ZClass reg. bug
  193.                   ) and d['id']==id),
  194.             self.aq_maybe('_getProductRegistryData')(type)
  195.             )
  196.  
  197.         self.aq_maybe('_setProductRegistryData')(type, tuple(values))
  198.  
  199.  
  200.  
  201. class ProductRegistry(ProductRegistryMixin):
  202.     # This class implements a protocol for registering products that
  203.     # are defined through the web.  It also provides methods for
  204.     # getting hold of the Product Registry, Control_Panel.Products.
  205.  
  206.     # This class is a mix-in class for the top-level application object.
  207.  
  208.     def _getProducts(self): return self.Control_Panel.Products
  209.  
  210.     _product_meta_types=()
  211.     _product_permissions=()
  212.     _product_ac_permissions=()
  213.  
  214.     _product_zclasses=() # product, id, meta_type, class
  215.  
  216.  
  217.     def _getProductRegistryMetaTypes(self): return self._product_meta_types
  218.     def _setProductRegistryMetaTypes(self, v): self._product_meta_types=v
  219.  
  220.     def _getProductRegistryData(self, name):
  221.         return getattr(self, '_product_%s' % name)
  222.  
  223.     def _setProductRegistryData(self, name, v):
  224.         name='_product_%s' % name
  225.         if hasattr(self, name):
  226.             return setattr(self, name, v)
  227.         else:
  228.             raise AttributeError, name
  229.  
  230.  
  231.